/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.data;

import java.net.URI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;

/*
 *
 */
public class TapTableTestCase
extends DataTestBase
    {

    protected TapService service ;

    @Before
    public void before()
        {
        super.before();
        log.debug("TapTableTestCase.before()");
        service = womble().services().create(
            unique(
                URI.create(
                    "ivo://org.astrodabis.test/service/"
                    )
                ),
            "test-service"
            );
        }

    @Test
    public void checkCreated()
        {
        TapTable created = service.tables().create(
            "test-table"
            );
        assertNotNull(
            created
            );
        assertNotNull(
            created.ident()
            );
        assertNotNull(
            created.name()
            );
        assertEquals(
            "test-table",
            created.name()
            );
        }    

    @Test
    public void checkCreatedTwiceReturnsSame()
        {
        compare(
            service.tables().create(
                "test-table"
                ),
            service.tables().create(
                "test-table"
                )
            );
        }

    @Test
    public void checkSelectFromService()
        {
        assertNotNull(
            service.tables().select()
            );
        assertNotNull(
            service.tables().select().iterator()
            );
        assertFalse(
            service.tables().select().iterator().hasNext()
            );
        assertNotNull(
            service.tables().create(
                "test-table"
                )
            );
        assertTrue(
            service.tables().select().iterator().hasNext()
            );
        assertNotNull(
            service.tables().select().iterator().next()
            );
        assertEquals(
            "test-table",
            service.tables().select().iterator().next().name()
            );
        }    

    @Test
    public void checkSelectFromServiceCount()
        {
        for (int count = 1 ; count <= 10 ; count++)
            {
            service.tables().create(
                unique(
                    "test-table"
                    )
                );
            assertEquals(
                count,
                count(
                    service.tables().select()
                    )
                );
            }    
        }    

    @Test
    public void checkSelectFromServiceCountPlusOne()
        {
        for (int count = 1 ; count <= 10 ; count++)
            {
            service.tables().create(
                "test-table"
                );
            service.tables().create(
                unique(
                    "test-table"
                    )
                );
            assertEquals(
                (count + 1),
                count(
                    service.tables().select()
                    )
                );
            }    
        }    


/*
    @Test
    public void checkSelectFromServiceByName()
        {
        assertNotNull(
            service.tables().select(
                "frog"
                )
            );
        assertNotNull(
            service.tables().select(
                "frog"
                ).iterator()
            );
        assertFalse(
            service.tables().select(
                "frog"
                ).iterator().hasNext()
            );
        assertFalse(
            service.tables().select(
                "toad"
                ).iterator().hasNext()
            );
        assertNotNull(
            service.tables().create(
                "frog"
                )
            );
        assertTrue(
            service.tables().select(
                "frog"
                ).iterator().hasNext()
            );
        assertFalse(
            service.tables().select(
                "toad"
                ).iterator().hasNext()
            );
        }    

    @Test
    public void checkSelectFromServiceByNameCount()
        {
        int frogs = 0 ;
        int toads = 0 ;
        for (int count = 0 ; count < 10 ; count++)
            {
            service.tables().create(
                "frog"
                );
            frogs++;

            if ((count % 2) == 0)
                {
                service.tables().create(
                    "toad"
                    );
                toads++;
                }

            assertEquals(
                (frogs + toads),
                count(
                    service.tables().select()
                    )
                );

            assertEquals(
                frogs,
                count(
                    service.tables().select(
                        "frog"
                        )
                    )
                );

            assertEquals(
                toads,
                count(
                    service.tables().select(
                        "toad"
                        )
                    )
                );
            }    
        }    
 */
    }

